import 'dart:async';

import 'package:flutter/material.dart';
import 'package:gold/app/user_provider.dart';
import 'package:gold/base/view_model.dart';
import 'package:gold/http/api_login.dart';
import 'package:gold/http/http.dart';
import 'package:gold/route/xfg_nav.dart';
import 'package:gold/utils/xfg_utils.dart';

/// @description:
/// @author tangzhentao
/// @created at 21:38 2024/11/24

class PasswordViewmodel extends Viewmodel {
  /// 是否登录页面进来
  final bool isLogin;

  /// 手机号
  final TextEditingController phoneController = TextEditingController();
  final FocusNode phoneNode = FocusNode();

  /// 验证码
  final TextEditingController codeController = TextEditingController();
  final FocusNode codeNode = FocusNode();

  /// 设置密码
  final TextEditingController psdController = TextEditingController();
  final FocusNode psdNode = FocusNode();

  /// 再次输入
  final TextEditingController againController = TextEditingController();
  final FocusNode againNode = FocusNode();

  /// 验证码计时器
  Timer? _timer;
  /// 验证码倒计时
  int _codeSecond = 60;
  int get codeSecond => _codeSecond;
  set codeSecond(int value) {
    if (_codeSecond != value) {
      _codeSecond = value;
      notifyListeners();
    }
  }

  /// 是否可点击
  bool _canClick = false;
  bool get canClick => _canClick;

  /// 发送验证码是否可以点击
  bool _canCodeClick = false;
  bool get canCodeClick => _canCodeClick;

  PasswordViewmodel(this.isLogin);

  /// 手机号变化
  void phoneChanged(String phone) {
    bool changed = false;
    final codeClick = isPhone(phone);
    if (_canCodeClick != codeClick) { _canCodeClick = codeClick; changed = true; }
    final click = codeClick && codeController.text.isNotEmpty && isCorrectPassword();
    if (_canClick != click) { _canClick = click; changed = true; }
    if (changed) notifyListeners();
  }

  /// 验证码变化
  void codeChanged(String code) {
    final click = isPhone(phoneController.text) && code.isNotEmpty && isCorrectPassword();
    if (_canClick != click) {
      _canClick = click;
      notifyListeners();
    }
  }

  /// 设置密码变化
  void psdChanged(String psd) {
    final click = isPhone(phoneController.text) && codeController.text.isNotEmpty && isCorrectPassword();
    if (_canClick != click) {
      _canClick = click;
      notifyListeners();
    }
  }

  /// 再次输入变化
  void againChanged(String again) {
    final click = isPhone(phoneController.text) && codeController.text.isNotEmpty && isCorrectPassword();
    if (_canClick != click) {
      _canClick = click;
      notifyListeners();
    }
  }

  /// 是否正确密码
  bool isCorrectPassword() {
    final psd = psdController.text;
    return psd == againController.text && psd.length >= 6 && psd.length <= 20;
  }

  /// 发送验证码
  Future sendCode() async {
    hideKeyboard();
    if (_codeSecond != 60) return;
    final result = await Http().request<String>(ApiLogin.sendSmsCode,
        data: {'mobile': phoneController.text, 'event': 'forgotPassword'}, successText: '已发送');
    if (result.success) {
      _startTimer();
    }
  }

  /// 登录
  Future done() async {
    hideKeyboard();
    final result = await Http().request<dynamic>(ApiLogin.forgotPassword,
        data: {'mobile': phoneController.text, 'captcha': codeController.text, 'password': psdController.text, 'confirm_password': againController.text},
        loadingText: '修改中...', successText: '修改成功，请登录~');
    if (result.success) {
      if (isLogin) {
        XfgNav.pop();
      } else {
        XfgNav.read<UserProvider>().signOut();
      }
    }
  }

  /// 开启计时器
  void _startTimer() {
    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      codeSecond--;
      if (codeSecond == 0) {
        _cancelTimer();
      }
    });
  }

  /// 停止计时器
  void _cancelTimer() {
    codeSecond = 60;
    _timer?.cancel();
    _timer = null;
  }

  @override
  void dispose() {
    _cancelTimer();
    hideKeyboard();
    phoneController.dispose();
    codeController.dispose();
    psdController.dispose();
    againController.dispose();
    super.dispose();
  }
}